using System;
using System.IO;
using System.Text;

namespace OracleInternal.SqlAndPlsqlParser
{
	internal class Service
	{
		public static string ReadFile(string file)
		{
			string result = string.Empty;
			InputStream inputStream = null;
			try
			{
				inputStream = new InputStream(file);
				result = inputStream.ReadToEnd();
				return result;
			}
			catch (Exception ex)
			{
				Logger.GetLogger("Service").Log(LoggerLevel.WARNING, ex.StackTrace, ex);
				return result;
			}
			finally
			{
				inputStream?.Close();
			}
		}

		public static void Copy(Uri url, string dstdir)
		{
			try
			{
				string absolutePath = url.AbsolutePath;
				string name = new FileInfo(absolutePath).Name;
				Stream stream = new FileStream(absolutePath, FileMode.Open, FileAccess.Read);
				Stream stream2 = new FileStream(new FileInfo(dstdir + Path.DirectorySeparatorChar + name).FullName, FileMode.Create);
				sbyte[] array = new sbyte[1024];
				int count;
				while ((count = SupportClass.ReadInput(stream, array, 0, array.Length)) > 0)
				{
					stream2.Write(SupportClass.ToByteArray(array), 0, count);
				}
				stream.Close();
				stream2.Close();
			}
			catch (IOException ex)
			{
				Logger.GetLogger("Messages").Log(LoggerLevel.WARNING, ex.StackTrace, ex);
			}
		}

		public static string ToNull(string src)
		{
			if (src != null && src.Length != 0)
			{
				return src;
			}
			return null;
		}

		public static string IndentLine(int level, string txt)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(' ', level);
			stringBuilder.Append(txt);
			return stringBuilder.ToString();
		}

		public static void IndentLine(int level, string txt, ref StringBuilder sb)
		{
			sb.Append(' ', level);
			sb.Append(txt);
		}

		public static int Pair(int x, int y)
		{
			return (y << 16) | x;
		}

		public static int Y(int p)
		{
			return (p >> 16) & 0xFFFF;
		}

		public static int X(int p)
		{
			return p & 0xFFFF;
		}

		public static long LongPair(int x, int y)
		{
			return ((long)y << 32) | (uint)x;
		}

		public static int LongY(long p)
		{
			return (int)(p >> 32);
		}

		public static int LongX(long p)
		{
			return (int)p;
		}

		public static int DecrementPair(int p)
		{
			return p - 65537;
		}

		public static string HandleMixedCase(string name)
		{
			if (name == null || name.Length == 0)
			{
				return name;
			}
			if (name[0] == '"')
			{
				int length = name.Length;
				if (length > 1 && name[length - 1] == '"')
				{
					name = name.Replace("\".\"", ".").Substring(1, length - 2);
				}
			}
			else
			{
				name = name.ToUpper();
			}
			return name;
		}

		public static string Into2Chars(string data)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (char i2 in data)
			{
				stringBuilder.Append((char)(PeriodicRemainder(i2, 16) + 70));
				stringBuilder.Append((char)(PeriodicDivision(i2, 16) + 70));
			}
			return stringBuilder.ToString();
		}

		public static string From2Chars(string data)
		{
			if (data == null)
			{
				return null;
			}
			int length = data.Length;
			if (length % 2 != 0)
			{
				return null;
			}
			StringBuilder stringBuilder = new StringBuilder();
			int num = 0;
			while (num < length)
			{
				stringBuilder.Append((char)((data[num++] - 70) * 16 + (data[num++] - 70)));
			}
			return stringBuilder.ToString();
		}

		internal static char[] Into2Chars(char input)
		{
			return new char[2]
			{
				(char)(PeriodicRemainder(input, 16) + 70),
				(char)(PeriodicDivision(input, 16) + 70)
			};
		}

		internal static int PeriodicRemainder(int i, int j)
		{
			int num = i % j;
			if (num < 0)
			{
				num += j;
			}
			return num;
		}

		internal static int PeriodicDivision(int i, int j)
		{
			int num = i / j;
			if (num < 0)
			{
				num--;
			}
			return num;
		}
	}
}
